<template>

<!--引用container布局-->
  <el-container class="home-container">
    <!--头部-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt class="logo_img" />
        <span>运动管理平台</span>
      </div>
      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <!--主题-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapsed?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapase">|||</div>
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff"
        unique-opened :collapse="isCollapsed" :collapse-transition="false" :router="true" :default-active="activePath">
<!--          一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObject[item.id]"></i>
              <span>{{item.title}}</span>
            </template>
<!--          二级菜单-->
            <el-menu-item :index="it.path" v-for="it in item.sList" :key="it.id"  @click="saveNavState(it.path+'')">
              <template slot="title">
                <i :class="iconsObject[it.id]"></i>
                <span>{{it.title}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
    <!--主体内容-->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </el-container>

</template>
<script>
export default {
  data(){
    return{
      menuList:[],
      isCollapsed:false,//伸缩属性
      iconsObject: {
        '100':'iconfont iconguanliyuan',
        '200':'iconfont iconsport',
        '101':'iconfont icondenglu',
        '102':'iconfont iconmima',
        '103':'iconfont iconsport',
        '104':'iconfont iconshangpin',
        '201':'iconfont iconshu',
        '202':'iconfont iconkaluli',
        '203':'iconfont iconshiwu',
        '204':'iconfont icondenglu',
      },

      // 被激活的连接
      activePath:'',

    }
  },
  //onload事件
  created(){
    //查询menuList
    this.getMenuList();
    this.activePath=window.sessionStorage.getItem('activePath');//取出session里的path

  },
  methods: {
    logout(){
      window.sessionStorage.clear();//清楚session
      this.$router.push("/login")//回到登陆首页
      this.$message.success("操作成功！！！");
    },
    //获取导航菜单方法
    async getMenuList(){
      const {data:res}=await this.$http.get("menus");
      console.log(res);
      if (res.flag != 200){
        return this.$message.error("获取列表失败！！！");
      }
      this.menuList=res.menus;
    },
    //控制伸缩
    toggleCollapase(){
      this.isCollapsed=!this.isCollapsed;
    },
    // 保存二级菜单的路径
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath);// 存放点击的二级菜单
      this.activePath = activePath;// 给点击的菜单添加高亮
    },
  }

}
</script>
<style lang="less" scoped>
//布局样式
.home-container {
  height: 100%;
}
//头部样式
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;// 左右贴边
  padding-left: 0%;// 左边界
  align-items: center;// 水平
  color: #fff;
  font-size: 20px;
  > div { //左侧div加布局
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
//侧边栏样式
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;// 对齐右边框
  }
}
//主体样式
.el-main {
  background-color: #eaedf1;
}
.logo_img {
  width: 66px;
  height: 66px;
}
.toggle-button{
  background-color:#4A5064;
  font-size: 10px;
  line-height: 24px;
  color:#fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;// 显示鼠标指针为：小手

}
</style>